package com.nanbei.thread;
import org.openjdk.jol.info.ClassLayout;


/**
 * @ClassDescription: 锁升级
 * @JdkVersion: 1.8
 * @Author: libs
 * @Created: 2024/10/27 16:49
 */
public class SynchronizedUpDemo {
    public static void main(String[] args) throws InterruptedException {
        // 无锁状态
        // noLock();

        // 偏向锁
        // 开启偏向锁 -XX:+UseBiasedLocking -XX:BiasedLockingStartupDelay=0
        // biasedLock();

        Object o = new Object();
        System.out.println(ClassLayout.parseInstance(o).toPrintable());

        synchronized (o) {
            System.out.println( ClassLayout.parseInstance(o).toPrintable());
            o.hashCode();
            System.out.println( ClassLayout.parseInstance(o).toPrintable());
        }


    }

    /**
     * 偏向锁默认开启延迟
     * -XX:+UseBiasedLocking   启用偏向锁
     * -XX:BiasedLockingStartupDelay=0  将偏向锁的延迟时间设置为0
     */
    private static void biasedLock() {
        Object o = new Object();
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
        new Thread(()->{
            synchronized (o) {
                System.out.println( ClassLayout.parseInstance(o).toPrintable());
            }
        },"t1").start();

        // ???
       /* synchronized (o) {
            System.out.println( ClassLayout.parseInstance(o).toPrintable());
        }*/
    }

    /**
     * 无锁状态
     */
    private static void noLock() {
        Object o = new Object();
        System.out.println(Integer.toHexString(o.hashCode()));//16进制
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
}
